package com.bnyy.medicalHousekeeper.moudle.message.adapter;

import android.content.Context;

import com.bnyy.message.bean.chat.Chat;

import java.util.ArrayList;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/12/8
 **/
public class IndexChatAdapter extends ChatAdapter {

    private ArrayList<Chat> toppingIndexChats = new ArrayList<>();
    private ArrayList<Chat> indexChats = new ArrayList<>();

    public IndexChatAdapter(Context context, Callback callback) {
        super(context, callback);
    }

    public void onDataChange(Chat newChat) {

        ArrayList<Chat> datas = getDatas();
        int index = -1; // 针对置顶列表和非置顶列表的下标，-1表示是新的会话
        String chatId = newChat.getChatId();
        ArrayList<Chat> toppingIndexChats = new ArrayList<>();

        for (int i = 0; i < datas.size(); i++) {
            Chat chat = datas.get(i);
            if (datas.get(i).getChatId().equals(chatId)) {
                index = i;
            }
            if (chat.getToppingTime() > 0) {
                toppingIndexChats.add(chat);
            }
        }

        int toppingIndexChatSize = toppingIndexChats.size();

        switch (newChat.getChatType()) {
            case GROUP_CHAT:
            case PRIVATE_CHAT:
                if (index == -1) {
                    if (newChat.getToppingTime() > 0) {
                        datas.add(0, newChat);
                        notifyDataSetChanged();
                    } else {
                        datas.add(toppingIndexChatSize, newChat);
                        notifyItemRangeChanged(toppingIndexChatSize, datas.size() - toppingIndexChatSize);
                    }
                } else {
                    Chat chat = datas.get(index);
                    if (chat.getToppingTime() > 0) {
                        datas.add(0, datas.remove(index));
                        notifyDataSetChanged();
                    } else {
                        if (index < toppingIndexChatSize) { // 从置顶移除
                            datas.add(toppingIndexChatSize, datas.remove(index));
                            notifyItemRangeChanged(index, toppingIndexChatSize + 1);
                        } else {
                            if (index == (datas.size() - toppingIndexChatSize - 1)) { // 原本就处于非置顶第一项
                                notifyItemChanged(index);
                            } else {
                                datas.add(toppingIndexChatSize, datas.remove(index));
                                notifyItemRangeChanged(toppingIndexChatSize, datas.size() - toppingIndexChatSize);
                            }
                        }
                    }
                }
                break;
            default:
                if (toppingIndexChatSize > 0) {
                    index -= toppingIndexChatSize;
                }
                if (index == 0) {
                    notifyItemChanged(index);
                } else {
                    datas.add(0, datas.remove(index));
                    notifyItemRangeChanged(0, index + 1);
                }
                break;
        }
    }

    public void refreshIndexChatItems(ArrayList<Chat> toppingIndexChats, ArrayList<Chat> indexChats) {
        if (toppingIndexChats == null || toppingIndexChats.size() == 0) {
            this.toppingIndexChats.clear();
        } else {
            this.toppingIndexChats = toppingIndexChats;
        }

        if (indexChats == null || indexChats.size() == 0) {
            this.indexChats.clear();
        } else {
            this.indexChats = indexChats;
        }

        ArrayList<Chat> datas = new ArrayList<>();
        datas.addAll(this.toppingIndexChats);
        datas.addAll(this.indexChats);

        refresh(datas);
    }
}
